---@class GT.event 事件
GT.event = {}

---@alias SELECT_UNIT fun(p:GT.player,u:GT.unit):void 选中单位
---@alias SELECT_UNIT_GROUP fun(p:GT.player,u_g:GT.unitGroup,team_id:Int32):void 选中单位
---@alias ADD_ITEM fun(u:GT.unit,i:GT.item):void 获得物品
---@alias REMOVE_ITEM fun(u:GT.unit,i:GT.item):void 失去物品
---@alias ABILITY_OBTAIN fun(u:GT.unit,a:GT.ability):void 获得技能
---@alias UNIT_BORN fun(u:GT.unit):void 单位出生-创建
---@alias UNIT_DIE fun(u:GT.unit):void 单位出生-死亡
---@alias ITEM_CREATE fun(i:GT.item):void 物品-创建
---@alias ABILITY_CS_START fun(a:GT.ability,u:GT.unit,tu:GT.unit,td:GT.destructible,ti:GT.item,tp:GT.point):void 技能-即将引导
---@alias ABILITY_PS_START fun(a:GT.ability,u:GT.unit,tu:GT.unit,td:GT.destructible,ti:GT.item,tp:GT.point):void 技能-施法开始
---@alias ABILITY_PS_END fun(a:GT.ability,u:GT.unit,tu:GT.unit,td:GT.destructible,ti:GT.item,tp:GT.point):void 技能-施法引导
---@alias ABILITY_SP_END fun(a:GT.ability,u:GT.unit,tu:GT.unit,td:GT.destructible,ti:GT.item,tp:GT.point):void 技能-施法出手
---@alias ABILITY_CST_END fun(a:GT.ability,u:GT.unit,tu:GT.unit,td:GT.destructible,ti:GT.item,tp:GT.point):void 技能-施法完成
---@alias ABILITY_BS_END fun(a:GT.ability,u:GT.unit,tu:GT.unit,td:GT.destructible,ti:GT.item,tp:GT.point):void 技能-后摇结束
---@alias ABILITY_LOSE fun(u:GT.unit,a:GT.ability):void 失去技能
---@alias ABILITY_END fun(a:GT.ability,u:GT.unit,tu:GT.unit,td:GT.destructible,ti:GT.item,tp:GT.point):void 技能-施法停止


---@alias ROLE_JOIN fun(p:GT.player,middle_join:boolean  ):void 玩家加入

---@alias PLAYER_EXIT fun(p:GT.player ):void 玩家退出
---@alias AREA_LEAVE fun(u:GT.unit ,area:GT.area|GT.rec_area|GT.cir_area|GT.poly_area):void 区域离开
---@alias AREA_ENTER fun(u:GT.unit ,area:GT.area|GT.rec_area|GT.cir_area|GT.poly_area):void 区域进入

---@alias UNIT_HURT_OTHER_FINISH fun(su:GT.unit,tu:GT.unit,d:number,d_type:DMGType,is_crit:boolean,is_attack:boolean,ability:GT.ability ):void 造成伤害(结算完成)-指定单位
---@alias UNIT_HURT_OTHER_COMPLETE fun(su:GT.unit,tu:GT.unit,d:number,d_type:DMGType,is_crit:boolean,is_attack:boolean,ability:GT.ability ):void 造成伤害(结算完成)-任意单位




---选中单位
---@param fun SELECT_UNIT
---@return table
function GT.event.SELECT_UNIT(fun)
    return y3.game:event(GlobalEventType['选中单位'], function(_, data)
        fun(GT.player.get_by_int(data.data.__role_id), GT.unit.get_unit_by_id(data.data.__unit_id))
    end)
end

---选中单位组
---@param fun SELECT_UNIT_GROUP
---@return table
function GT.event.SELECT_UNIT_GROUP(fun)
    return y3.game:event(GlobalEventType['选中单位组'], function(_, data)
        fun(GT.player.get_by_int(data.data.__role_id), GT.unitGroup.obj(data.data.__unit_group_id_list),
            data.data.__team_id)
    end)
end

---物品-单位获得
---@param fun ADD_ITEM
---@return table
function GT.event.ADD_ITEM(fun)
    return y3.game:event(ItemEventType['物品-单位获得'], function(_, data)
        -- - "<var>" = {
        --     -     "data" = {'__unit_id': 6, '__item_id': 8, '__item_no': 134257216}
        --     - }
        fun(GT.unit.get_unit_by_id(data.data.__unit_id), GT.item.get_item(data.data.__item_id))
    end)
end

---物品-单位失去
---@param fun REMOVE_ITEM
---@return table
function GT.event.REMOVE_ITEM(fun)
    return y3.game:event(ItemEventType['物品-单位失去'], function(_, data)
        fun(GT.unit.get_unit_by_id(data.data.__unit_id), GT.item.get_item(data.data.__item_id))
    end)
end

---物品-创建
---@param fun ITEM_CREATE
---@return table
function GT.event.ITEM_CREATE(fun)
    return y3.game:event(ItemEventType['物品创建'], function(_, data)
        -- - "<var>" = {
        --     -     "data" = {'__item': <<Item-no(134257216):id(11),cnt(1)>>, '__item_id': 11}
        --     - }
        fun(GT.item.get_item(data.data.__item_id))
    end)
end

---获得技能
---@param fun ABILITY_OBTAIN
---@return table
function GT.event.ABILITY_OBTAIN(fun)
    return y3.game:event(AbilityEventType['获得技能'], function(_, data)
        -- {'__ability': Ability<134271767>, '__cur_ability': Ability<134271767>, '__ability_seq': 8, '__ability_type': 3, '__ability_index': 5, '__ability_id': 134271767, '__unit_id': 6}
        fun(GT.unit.get_unit_by_id(data.data.__unit_id), GT.ability.obj(data.data.__ability))
    end)
end

---单位出生
---@param fun UNIT_BORN
---@return table
function GT.event.UNIT_BORN(fun)
    return y3.game:event(UnitEventType['单位出生'], function(_, data)
        -- - "<var>" = {
        --     -     "data" = {'__unit_id': 6}
        --     - }
        fun(GT.unit.get_unit_by_id(data.data.__unit_id))
    end)
end

---单位死亡
---@param fun UNIT_DIE
---@return table
function GT.event.UNIT_DIE(fun)
    return y3.game:event(UnitEventType['单位死亡'], function(_, data)
        fun(GT.unit.get_unit_by_id(data.data.__unit_id))
    end)
end

---即将施法
---@param fun ABILITY_CS_START
---@return table
function GT.event.ABILITY_CS_START(fun)
    return y3.game:event(AbilityEventType['即将施法'], function(_, data)
        -- - "<var>" = {
        --     -     "data" = {'__ability': Ability<134276941>, '__cur_ability': Ability<134276941>, '__ability_seq': 5, '__ability_type': 3, '__ability_index': 4, '__ability_id': 134276941, '__unit_id': 6, '__ability_runtime_id': 1, '__ability_target_unit_id': 0}
        --     - }
        local a = GT.ability.obj(data.data.__ability)
        local runtime_id = data.data.__ability_runtime_id
        fun(a, GT.unit.get_unit_by_id(data.data.__unit_id), a:get_target_unit_in_ability(runtime_id),
            a:get_target_dest_in_ability(runtime_id), a:get_target_item_in_ability(runtime_id),
            a:api_get_release_position(runtime_id))
    end)
end

---施法开始
---@param fun ABILITY_PS_START
---@return table
function GT.event.ABILITY_PS_START(fun)
    return y3.game:event(AbilityEventType['施法开始'], function(_, data)
        -- - "<var>" = {
        --     -     "data" = {'__ability': Ability<134262581>, '__cur_ability': Ability<134262581>, '__ability_seq': 1, '__ability_type': 1, '__ability_index': 1, '__ability_id': 134262581, '__unit_id': 6, '__ability_runtime_id': 1, '__ability_target_unit_id': 7}
        --     - }

        local a = GT.ability.obj(data.data.__ability)
        local runtime_id = data.data.__ability_runtime_id
        fun(a, GT.unit.get_unit_by_id(data.data.__unit_id), a:get_target_unit_in_ability(runtime_id),
            a:get_target_dest_in_ability(runtime_id), a:get_target_item_in_ability(runtime_id),
            a:api_get_release_position(runtime_id))
    end)
end

---施法引导
---@param fun ABILITY_PS_END
---@return table
function GT.event.ABILITY_PS_END(fun)
    return y3.game:event(AbilityEventType['施法引导'], function(_, data)
        -- - "<var>" = {
        --     -     "data" = {'__ability': Ability<134262581>, '__cur_ability': Ability<134262581>, '__ability_seq': 1, '__ability_type': 1, '__ability_index': 1, '__ability_id': 134262581, '__unit_id': 6, '__ability_runtime_id': 1, '__ability_target_unit_id': 7}
        --     - }
        local a = GT.ability.obj(data.data.__ability)
        local runtime_id = data.data.__ability_runtime_id
        fun(a, GT.unit.get_unit_by_id(data.data.__unit_id), a:get_target_unit_in_ability(runtime_id),
            a:get_target_dest_in_ability(runtime_id), a:get_target_item_in_ability(runtime_id),
            a:api_get_release_position(runtime_id))
    end)
end

---施法出手
---@param fun ABILITY_SP_END
---@return table
function GT.event.ABILITY_SP_END(fun)
    return y3.game:event(AbilityEventType['施法出手'], function(_, data)
        -- - "<var>" = {
        --     -     "data" = {'__ability': Ability<134262581>, '__cur_ability': Ability<134262581>, '__ability_seq': 1, '__ability_type': 1, '__ability_index': 1, '__ability_id': 134262581, '__unit_id': 6, '__ability_runtime_id': 1, '__ability_target_unit_id': 7}
        --     - }
        local a = GT.ability.obj(data.data.__ability)
        local runtime_id = data.data.__ability_runtime_id
        -- print_debug(data.data.__unit_id)
        fun(a, GT.unit.get_unit_by_id(data.data.__unit_id), a:get_target_unit_in_ability(runtime_id),
            a:get_target_dest_in_ability(runtime_id), a:get_target_item_in_ability(runtime_id),
            a:api_get_release_position(runtime_id))
    end)
end

---施法完成
---@param fun ABILITY_CST_END
---@return table
function GT.event.ABILITY_CST_END(fun)
    return y3.game:event(AbilityEventType['施法完成'], function(_, data)
        -- - "<var>" = {
        --     -     "data" = {'__ability': Ability<134262581>, '__cur_ability': Ability<134262581>, '__ability_seq': 1, '__ability_type': 1, '__ability_index': 1, '__ability_id': 134262581, '__unit_id': 6, '__ability_runtime_id': 1, '__ability_target_unit_id': 7}
        --     - }
        local a = GT.ability.obj(data.data.__ability)
        local runtime_id = data.data.__ability_runtime_id
        fun(a, GT.unit.get_unit_by_id(data.data.__unit_id), a:get_target_unit_in_ability(runtime_id),
            a:get_target_dest_in_ability(runtime_id), a:get_target_item_in_ability(runtime_id),
            a:api_get_release_position(runtime_id))
    end)
end

---后摇结束
---@param fun ABILITY_BS_END
---@return table
function GT.event.ABILITY_BS_END(fun)
    return y3.game:event(AbilityEventType['后摇结束'], function(_, data)
        -- - "<var>" = {
        --     -     "data" = {'__ability': Ability<134262581>, '__cur_ability': Ability<134262581>, '__ability_seq': 1, '__ability_type': 1, '__ability_index': 1, '__ability_id': 134262581, '__unit_id': 6, '__ability_runtime_id': 1, '__ability_target_unit_id': 7}
        --     - }
        local a = GT.ability.obj(data.data.__ability)
        local runtime_id = data.data.__ability_runtime_id
        fun(a, GT.unit.get_unit_by_id(data.data.__unit_id), a:get_target_unit_in_ability(runtime_id),
            a:get_target_dest_in_ability(runtime_id), a:get_target_item_in_ability(runtime_id),
            a:api_get_release_position(runtime_id))
    end)
end

---施法停止
---@param fun ABILITY_END
---@return table
function GT.event.ABILITY_END(fun)
    return y3.game:event(AbilityEventType['施法停止'], function(_, data)
        -- - "<var>" = {
        --     -     "data" = {'__ability': Ability<134262581>, '__cur_ability': Ability<134262581>, '__ability_seq': 1, '__ability_type': 1, '__ability_index': 1, '__ability_id': 134262581, '__unit_id': 6, '__ability_runtime_id': 1, '__ability_target_unit_id': 7}
        --     - }
        local a = GT.ability.obj(data.data.__ability)
        local runtime_id = data.data.__ability_runtime_id
        fun(a, GT.unit.get_unit_by_id(data.data.__unit_id), a:get_target_unit_in_ability(runtime_id),
            a:get_target_dest_in_ability(runtime_id), a:get_target_item_in_ability(runtime_id),
            a:api_get_release_position(runtime_id))
    end)
end

---技能失去
---@param fun ABILITY_LOSE
---@return table
function GT.event.ABILITY_LOSE(fun)
    return y3.game:event(AbilityEventType['失去技能'], function(_, data)
        fun(GT.unit.get_unit_by_id(data.data.__unit_id), GT.ability.obj(data.data.__ability))
    end)
end

---玩家加入
---@param fun ROLE_JOIN
---@return table
function GT.event.ROLE_JOIN(fun)
    return y3.game:event(GlobalEventType['玩家加入'], function(_, data)
        -- - "<var>" = {
        --     -     "data" = {'__role_id': 1, '__is_middle_join': False}
        --     - }

        fun(GT.player.get_by_int(data.data.__role_id), data.data.__is_middle_join)
    end)
end

---玩家退出
---@param fun PLAYER_EXIT
---@return table
function GT.event.PLAYER_EXIT(fun)
    return y3.game:event(GlobalEventType['玩家退出'], function(_, data)
        fun(GT.player.get_by_int(data.data.__role_id))
    end)
end

---区域离开
---@param area GT.area|GT.rec_area|GT.cir_area|GT.poly_area
---@param fun AREA_LEAVE
---@return table
function GT.event.AREA_LEAVE(area, fun)
    return y3.game:event({ GlobalEventType['区域离开'], function(trigger, actor) return area.area end },
        function(_, data)
            -- - "<var>" = {
            --     -     "data" = {'__unit_id': 10000001, '__area_id': 10000, '__trigger_id': 50068}
            --     - }
            fun(GT.unit.get_unit_by_id(data.data.__unit_id), area)
        end)
end

---区域进入
---@param area GT.area|GT.rec_area|GT.cir_area|GT.poly_area
---@param fun AREA_ENTER
---@return table
function GT.event.AREA_ENTER(area, fun)
    return y3.game:event({ GlobalEventType['区域进入'], function(trigger, actor) return area.area end },
        function(_, data)
            -- - "<var>" = {
            --     -     "data" = {'__unit_id': 10000001, '__area_id': 10000, '__trigger_id': 50068}
            --     - }
            fun(GT.unit.get_unit_by_id(data.data.__unit_id), area)
        end)
end

---指定单位 造成伤害(结算完成)
---@param u GT.unit 单位
---@param fun UNIT_HURT_OTHER_FINISH
---@return table
function GT.event.UNIT_HURT_OTHER_FINISH(u, fun)
    return y3.game:event({ UnitEventType['造成伤害(结算完成)-指定单位'],
            function(trigger, actor) return u.unit end },
        function(_, data)
            -- - "<var>" = {
            --     -     "data" = {'__source_unit': <LCreature(4)>, '__target_unit': <LCreature(1)>, '__damage_type': 0, '__ability': Ability<134262581>, '__is_normal_hit': True, 'critical': False, 'no_miss': False, '__damage': framecore.fixmath.Fix32([H]'-55'), 'damage_params': framecore.fixmath.Fix32([H]'-55'), '__jump_word_type': 0, '__forbid_critical_hit': False, '__damage_result_state': 1, 'hurt_coffcient': framecore.fixmath.Fix32([H]'1'), 'unit_id': 4, 'once_damage': framecore.fixmath.Fix32([H]'-55'), 'hp_vampire': framecore.fixmath.Fix32([H]'0'), 'hp_changed': framecore.fixmath.Fix32([H]'-55'), '__is_critical_hit': False}
            --     - }
            fun(GT.unit.obj(data.data.__source_unit), GT.unit.obj(data.data.__target_unit), data.data.__damage:float(),
                data.data.__damage_type, data.data.__is_critical_hit, data.data.__is_normal_hit,
                GT.ability.obj(data.data.__ability))
        end)
end

---任意单位单位 造成伤害(结算完成)
---@param fun UNIT_HURT_OTHER_COMPLETE
---@return table
function GT.event.UNIT_HURT_OTHER_COMPLETE(fun)
    return y3.game:event(UnitEventType['造成伤害(结算完成)-任意单位'],
        function(_, data)
            -- - "<var>" = {
            --     -     "data" = {'__source_unit': <LCreature(4)>, '__target_unit': <LCreature(1)>, '__damage_type': 0, '__ability': Ability<134262581>, '__is_normal_hit': True, 'critical': False, 'no_miss': False, '__damage': framecore.fixmath.Fix32([H]'-55'), 'damage_params': framecore.fixmath.Fix32([H]'-55'), '__jump_word_type': 0, '__forbid_critical_hit': False, '__damage_result_state': 1, 'hurt_coffcient': framecore.fixmath.Fix32([H]'1'), 'unit_id': 4, 'once_damage': framecore.fixmath.Fix32([H]'-55'), 'hp_vampire': framecore.fixmath.Fix32([H]'0'), 'hp_changed': framecore.fixmath.Fix32([H]'-55'), '__is_critical_hit': False}
            --     - }
            fun(GT.unit.obj(data.data.__source_unit), GT.unit.obj(data.data.__target_unit), data.data.__damage:float(),
                data.data.__damage_type, data.data.__is_critical_hit, data.data.__is_normal_hit,
                GT.ability.obj(data.data.__ability))
        end)
end

---@alias OBTAIN_MODIFIER fun(owner_unit:GT.unit,from_unit:GT.unit,modifier:GT.modifier):void 获得魔法效果

---获得魔法效果
---@param fun OBTAIN_MODIFIER
---@return table
function GT.event.OBTAIN_MODIFIER(fun)
    return y3.game:event(ModifierEventType['获得魔法效果'],
        function(_, data)
            -- - "<var>" = {
            --     -     "data" = {'__owner_unit': <LCreature(4)>, '__from_unit_id': 0, '__cur_modifier': Modifier<134240918>, '__modifier': Modifier<134240918>}
            --     - }
           
            fun(GT.unit.obj(data.data.__owner_unit), GT.unit.get_unit_by_id(data.data.__from_unit_id),
                GT.modifier.obj(data.data.__modifier))
        end)
end

---@alias LOSS_MODIFIER fun(owner_unit:GT.unit,from_unit:GT.unit,modifier:GT.modifier):void 失去魔法效果

---失去魔法效果
---@param fun LOSS_MODIFIER
---@return table
function GT.event.LOSS_MODIFIER(fun)
    return y3.game:event(ModifierEventType['失去魔法效果'],
        function(_, data)
            -- - "<var>" = {
            --     -     "data" = {'__owner_unit': <LCreature(4)>, '__from_unit_id': 0, '__cur_modifier': Modifier<134240918>, '__modifier': Modifier<134240918>}
            --     - }

            fun(GT.unit.obj(data.data.__owner_unit),GT.unit.get_unit_by_id(data.data.__from_unit_id) ,
                GT.modifier.obj(data.data.__modifier))
        end)
end

---@alias MODIFIER_CYCLE_TRIGGER fun(owner_unit:GT.unit,from_unit:GT.unit,modifier:GT.modifier):void 失去魔法效果

---循环触发魔法效果
---@param fun MODIFIER_CYCLE_TRIGGER
---@return table
function GT.event.MODIFIER_CYCLE_TRIGGER(fun)
    return y3.game:event(ModifierEventType['循环触发'],
        function(_, data)
            -- - "<var>" = {
            --     -     "data" = {'__owner_unit': <LCreature(4)>, '__from_unit_id': 0, '__cur_modifier': Modifier<134240918>, '__modifier': Modifier<134240918>}
            --     - }
            fun(GT.unit.obj(data.data.__owner_unit), GT.unit.get_unit_by_id(data.data.__from_unit_id),
                GT.modifier.obj(data.data.__modifier))
        end)
end

---@alias MODIFIER_LAYER_CHANGE fun(owner_unit:GT.unit,from_unit:GT.unit,modifier:GT.modifier,value:number):void 失去魔法效果

---层数变化 魔法效果
---@param fun MODIFIER_LAYER_CHANGE
---@return table
function GT.event.MODIFIER_LAYER_CHANGE(fun)
    return y3.game:event(ModifierEventType['层数变化'],
        function(_, data)
            -- - "<var>" = {
            --     -     "data" = {'__owner_unit': <LCreature(4)>, '__from_unit_id': 0, '__modifier': Modifier<134240918>, '__layer_change_values': 1}
            --     - }
            fun(GT.unit.obj(data.data.__owner_unit), GT.unit.get_unit_by_id(data.data.__from_unit_id),
                GT.modifier.obj(data.data.__modifier), data.data.__layer_change_values)
        end)
end

---@alias MODIFIER_ADDTION fun(owner_unit:GT.unit,from_unit:GT.unit,modifier:GT.modifier,value:number):void 失去魔法效果

---叠加变化 魔法效果 --暂时无用
---@param fun MODIFIER_ADDTION
---@return table
function GT.event.MODIFIER_ADDTION(fun)
    return y3.game:event(ModifierEventType['叠加'],
        function(_, data)
            fun(GT.unit.obj(data.data.__owner_unit), GT.unit.get_unit_by_id(data.data.__from_unit_id),
                GT.modifier.obj(data.data.__modifier), data.data.__layer_change_values)
        end)
end
